package cn.gzmli.dtt.sbzyc.exception;

import cn.gzmli.dtt.common.entity.Result;
import cn.gzmli.dtt.common.utils.SpringContextUtil;
import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Descript
 * @Author xsx
 * @Date 2017/12/25
 */
@Component
@Aspect
public class ExceptionHandler {

    @Around(value = "execution(* com.daotantan.controller..*.*(..))")
    public Object resultForDaotantan(ProceedingJoinPoint pjp) {
        return procExceptions(pjp);
    }

    @Around(value = "execution(* com.emms.controller..*.*(..))")
    public Object resultForEmms(ProceedingJoinPoint pjp) {
        return procExceptions(pjp);
    }

    /**
     * 异常处理
     *
     * @param pjp
     * @return
     */
    public Object procExceptions(ProceedingJoinPoint pjp) {
        Result result;
        Logger log = Logger.getLogger(pjp.getTarget().getClass());
        try {
            Object o = pjp.proceed(pjp.getArgs());
            if (o instanceof Result) {
                result = (Result) o;
            } else {
                return o;
            }
        } catch (Throwable e) {
            e.printStackTrace();
            JdbcTemplate jdbcTemplate = SpringContextUtil.getBean("jdbcTemplate", JdbcTemplate.class);
            result = new Result(0, "操作失败,原因为:" + e.toString());
            StackTraceElement[] st = e.getStackTrace();
            List<String> args = new ArrayList<>();

            StringBuffer fileNames = new StringBuffer("");
            StringBuffer classNames = new StringBuffer("");
            StringBuffer methodNames = new StringBuffer("");
            StringBuffer lineNumbers = new StringBuffer("");
            StringBuffer fullExceptionMsg = new StringBuffer("");
            for (StackTraceElement stackTraceElement : st) {
                String exclass = stackTraceElement.getClassName();
                String method = stackTraceElement.getMethodName();
                String fileName = stackTraceElement.getFileName();
                Integer lineNumber = stackTraceElement.getLineNumber();
                if ((exclass.indexOf("com.daotantan") != -1
                        || exclass.indexOf("com.emms") != -1)) {
                    fileNames.append(fileName).append(",");
                    classNames.append(exclass).append(",");
                    methodNames.append(method).append(",");
                    lineNumbers.append(lineNumber).append(",");

                }
                fullExceptionMsg.append(fileName + "\r\n")
                        .append(exclass + "\r\n")
                        .append(method + "\r\n")
                        .append(lineNumber + "\r\n");

              /*  System.out.println(fileName);
                System.out.println(exclass);
                System.out.println(method);
                System.out.println(lineNumber);*/
            }

            if (fileNames.length() == 0) {
                args.add(null);
            } else {
                args.add(fileNames.substring(0, fileNames.length() - 1));
            }
            if (classNames.length() == 0) {
                args.add(null);
            } else {
                args.add(classNames.substring(0, classNames.length() - 1));
            }
            if (methodNames.length() == 0) {
                args.add(null);
            } else {
                args.add(methodNames.substring(0, methodNames.length() - 1));
            }
            if (lineNumbers.length() == 0) {
                args.add(null);
            } else {
                args.add(lineNumbers.substring(0, lineNumbers.length() - 1));
            }

            String sql = "INSERT INTO exceptionlog(fileName,className,methodName,lineNumber,fullMsg) VALUES(?,?,?,?,?)";
            args.add(fullExceptionMsg.toString());
            jdbcTemplate.update(sql, args.toArray());
            System.out.println(e);
            //throw new SystemException(e);
        } finally {

        }
        return result;
    }
}
